package com.dense.kuiniu.floatcabinet.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Book;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.BorrowBackResult;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.BorrowBook;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookBackAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookBorrowAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.SpaceItemDecoration;
import com.jess.arms.di.component.AppComponent;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/10/28
 */
public class BorrowCompleteActivity extends ImmerseActivity {

    @BindView(R.id.group_borrow)
    Group mGroupBorrow;
    @BindView(R.id.group_return)
    Group mGroupReturn;
    @BindView(R.id.tv_borrow_count)
    TextView mTvBorrowCount;
    @BindView(R.id.tv_return_count)
    TextView mTvReturnCount;
    @BindView(R.id.recycler_view_borrow)
    RecyclerView mRecyclerViewBorrow;
    @BindView(R.id.recycler_view_return)
    RecyclerView mRecyclerViewReturn;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_borrow_complete;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            BorrowBackResult result = intent.getParcelableExtra("result");
            if (result != null) {
                List<BorrowBook> borrowBooks = result.getBorrowList();
                List<Book> returnBooks = result.getBackList();
                if (borrowBooks == null || borrowBooks.isEmpty()) {
                    mGroupBorrow.setVisibility(View.GONE);
                } else {
                    mTvBorrowCount.setText(String.format(Locale.CHINA, "共计借阅：%d本", borrowBooks.size()));
                    mRecyclerViewBorrow.setLayoutManager(new GridLayoutManager(this, 2));
                    mRecyclerViewBorrow.addItemDecoration(new SpaceItemDecoration(20));
                    BookBorrowAdapter borrowAdapter = new BookBorrowAdapter(R.layout.item_book_borrow);
                    mRecyclerViewBorrow.setAdapter(borrowAdapter);
                    borrowAdapter.setNewInstance(borrowBooks);
                }
                if (returnBooks == null || returnBooks.isEmpty()) {
                    mGroupReturn.setVisibility(View.GONE);
                } else {
                    mTvReturnCount.setText(String.format(Locale.CHINA, "共计归还：%d本", returnBooks.size()));
                    mRecyclerViewReturn.setLayoutManager(new GridLayoutManager(this, 2));
                    mRecyclerViewReturn.addItemDecoration(new SpaceItemDecoration(20));
                    BookBackAdapter bookBackAdapter = new BookBackAdapter(R.layout.item_book_borrow);
                    mRecyclerViewReturn.setAdapter(bookBackAdapter);
                    bookBackAdapter.setNewInstance(returnBooks);
                }
            }
        }
    }

    @OnClick({R.id.tv_back, R.id.btn_return})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
            case R.id.btn_return:
                finish();
                break;
        }
    }
}